package com.bw.miaosha.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.miaosha.dao.GoodsDao;
import com.bw.miaosha.entity.GoodsEntity;
import com.bw.miaosha.entity.MiaoshaOrderEntity;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.miaosha.dao.MiaoshaActivityDao;
import com.bw.miaosha.entity.MiaoshaActivityEntity;
import com.bw.miaosha.service.MiaoshaActivityService;


@Service("miaoshaActivityService")
public class MiaoshaActivityServiceImpl extends ServiceImpl<MiaoshaActivityDao, MiaoshaActivityEntity> implements MiaoshaActivityService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String page1 = (String) params.get("page");
        IPage<MiaoshaActivityEntity> page=new Page<>();
        List<MiaoshaActivityEntity> records;
        if (page1.equals("1")){
            String activity = redisTemplate.opsForValue().get("activity");
            if (activity!=null && StringUtils.isNotEmpty(activity)){
                records = JSON.parseObject(activity, new TypeReference<List<MiaoshaActivityEntity>>() {
                });
                page.setRecords(records);
            }else{
                page = this.page(
                        new Query<MiaoshaActivityEntity>().getPage(params),
                        new QueryWrapper<MiaoshaActivityEntity>()
                );
                records = page.getRecords();
                for (MiaoshaActivityEntity miaoshaActivityEntity : records) {
                    Long goodsId = miaoshaActivityEntity.getGoodsId();
                    GoodsEntity goodsEntity = goodsDao.selectById(goodsId);
                    ArrayList<GoodsEntity> goodsEntitiess = new ArrayList<>();
                    goodsEntitiess.add(goodsEntity);
                    miaoshaActivityEntity.setGoodsName(goodsEntity.getGoodsName());
                    miaoshaActivityEntity.setGoodsImg(goodsEntity.getGoodsImg());
                    miaoshaActivityEntity.setList(goodsEntitiess);
                }
                String jsonString = JSON.toJSONString(records);
                 redisTemplate.opsForValue().set("activity",jsonString);

            }
            return  new PageUtils(page);
        }else {
            page = this.page(
                    new Query<MiaoshaActivityEntity>().getPage(params),
                    new QueryWrapper<MiaoshaActivityEntity>()
            );
            //12345678978122
            records = page.getRecords();
            for (MiaoshaActivityEntity miaoshaActivityEntity : records) {
                Long goodsId = miaoshaActivityEntity.getGoodsId();
                GoodsEntity goodsEntity = goodsDao.selectById(goodsId);
                ArrayList<GoodsEntity> goodsEntitiess = new ArrayList<>();
                goodsEntitiess.add(goodsEntity);
                miaoshaActivityEntity.setGoodsName(goodsEntity.getGoodsName());
                miaoshaActivityEntity.setGoodsImg(goodsEntity.getGoodsImg());
                miaoshaActivityEntity.setList(goodsEntitiess);
            }
        }

       return  new PageUtils(page);
    }

}